<?php
// +----------------------------------------------------------------------
// | OneThink [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.onethink.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: 麦当苗儿 <zuojiazi@vip.qq.com> <http://www.zjzit.cn>
// +----------------------------------------------------------------------
namespace Api\Controller;
/**
 * 文件控制器
 * 主要用于下载模型的文件上传和下载
 */
class FileController extends ApiController {
    /**
     * 上传图片
     * @author huajie <banhuajie@163.com>
     */
    public function uploadPicture(){
        /* 调用文件上传组件上传文件 */
        $Picture = D('Picture');
        $pic_driver = C('PICTURE_UPLOAD_DRIVER');
        $info = $Picture->upload(
            $_FILES,
            C('PICTURE_UPLOAD'),
            C('PICTURE_UPLOAD_DRIVER'),
            C("UPLOAD_{$pic_driver}_CONFIG")
        ); //TODO:上传到远程服务器
        !$info && response(-9);

        $info ? response(1, $info) : response(-9);
    }

    /**
     * 上传图片
     * @author huajie <banhuajie@163.com>
     */
    public function uploadPictureApp(){
        /* 调用文件上传组件上传文件 */
        $Picture = D('Picture');
        $pic_driver = C('PICTURE_UPLOAD_DRIVER');
        $info = $Picture->uploadApp(
            $_FILES,
            C('PICTURE_UPLOAD'),
            C('PICTURE_UPLOAD_DRIVER'),
            C("UPLOAD_{$pic_driver}_CONFIG")
        ); //TODO:上传到远程服务器
        !$info && response(-9,"");
        $info ? response(1, $info) : response(-9,"");
    }

    /**
     * 获取七牛token
     */
    function getQNtoken(){

        $config=C('UPLOAD_SITEIMG_QINIU');
        //var_dump(C('UPLOAD_SITEIMG_QINIU'));exit;
        $qiniu = new  \Think\QiniuStorage($config);
        $token = $qiniu->UploadToken($config);
        //echo json_encode(array("ret"=>200,'data'=>array("token"=>$token),'msg'=>''));
        $data['token']=$token;
        $data['url'] = $config['domain'];
        response(1,$data);
    }

    /**
     * 获取七牛token
     * @param key
     */
    public function UploadQN(){
        $config = C('UPLOAD_SITEIMG_QINIU');
        $qiniu = new  \Think\QiniuStorage($config);
        $files = $_FILES;
        foreach ($files as $key=>&$file) {
            $filename=time();
            $name=$file['name'];
            $file = array(
                'name' => 'file',
                'fileName' => $filename.".mp4",
                'fileBody' => file_get_contents($file['tmp_name'])
            );
            $config = array();
            $result = $qiniu->upload($config, $file);
            $data = array(
                'name' =>$name,
                'savename' => $result['key'],
                'create_time'=>NOW_TIME
            );
            $id = M('movie')->add($data);
            response(1,$id);
        }
    }

    /**
     * 上传七牛图片
     */
    public  function UploadQNpic(){
        $config1 = C('UPLOAD_SITEIMG_QINIU');
        $qiniu = new  \Think\QiniuStorage($config1);
        $files = $_FILES;
        foreach ($files as $key=>&$file) {
            $files = $_FILES['file']['tmp_name'];//文件
            $extend = explode('.', $_FILES['file']['name'])['1'];//后缀
            $objectFile = time().".".$extend;
            $content = file_get_contents($files);

            $file = array(
                'name' => 'file',
                'fileName' => $objectFile,
                'fileBody' => $content
            );
            $config = array();
            $result = $qiniu->upload($config, $file);
            $data = array(
                'path' =>$objectFile,
                'url' =>"http://".$config1['domain'].'/'.$result['key'],
                'create_time'=>NOW_TIME
            );
            $id = M('picture')->add($data);
            $data['file']['id']=$id;
            response(1,$data);

        }
    }
}
